package com.example.multithread.communication;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Phaser;
import java.util.stream.IntStream;

/**
 * @ClassName PhaserDemo
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2020/5/15 9:41 AM
 * @Version 1.0
 */
@Slf4j
public class PhaserDemo {
	public static void main(String[] args) {
		demo01();
	}

	static void demo01() {
		Phaser phaser = new Phaser();
		IntStream.rangeClosed(1, 10).forEach(i->{
			phaser.register();//注册10个线程
			new Thread(()->{
				log.info("我是{}, 我被挡住了.", i);
				int i1 = phaser.arriveAndAwaitAdvance();//指定当前线程到了栅栏位置.arrive+1, 直到arrive=10, 才继续.
				log.info("{}的阶段是: {}",i, i1);
			}).start();
		});
	}
}
